﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "LayoutOrViewportParametersCustomizer.h"
#include "DrawingBuiltInParameterDefinitions.h"
#include "IModelView.h"
#include "IViewClipRange.h"
#include "IDrawingLayout.h"
#include "IDrawingViewport.h"
#include "IGenericElement.h"
#include "IElementBasicInformation.h"
#include "IExternalDataComponent.h"
#include "IExternalData.h"
#include "IParameter.h"
#include "NdbUpgrade.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

BEGIN_UPGRADE_CLASS(gcmp, LayoutParametersCustomizer, 2)
CHANGE_PARENT(gcmp::ElementParametersCustomizer, E65C9C26 - 7D2A - 4AB6 - AFA2 - 9A51606C3D3C, gcmp::GbmpElementParametersCustomizer, D1442E2A - B664 - 4C85 - BD79 - CF17A4C692EB)
END_UPGRADE_CLASS

BEGIN_UPGRADE_CLASS(gcmp, ViewPortParametersCustomizer, 2)
CHANGE_PARENT(gcmp::ElementParametersCustomizer, E65C9C26 - 7D2A - 4AB6 - AFA2 - 9A51606C3D3C, gcmp::GbmpElementParametersCustomizer, D1442E2A - B664 - 4C85 - BD79 - CF17A4C692EB)
END_UPGRADE_CLASS

BEGIN_UPGRADE_CLASS(gcmp, DrawingViewportTypeParametersCustomizer, 2)
CHANGE_PARENT(gcmp::ElementParametersCustomizer, E65C9C26 - 7D2A - 4AB6 - AFA2 - 9A51606C3D3C, gcmp::GbmpElementParametersCustomizer, D1442E2A - B664 - 4C85 - BD79 - CF17A4C692EB)
END_UPGRADE_CLASS

DBOBJECT_DATA_DEFINE(GbmpElementParametersCustomizer)
{
}

DBOBJECT_DATA_DEFINE(LayoutParametersCustomizer)
{
    SetOwnerElement(nullptr);
}

IDocument* LayoutParametersCustomizer::GetDocument() const
{
    if (!m_pOwnerElement)
    {
        return nullptr;
    }
    return m_pOwnerElement->GetDocument();
}

bool LayoutParametersCustomizer::IsParameterModifiable(int paramDefId) const
{
    const IElement* pOwnerElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pOwnerElement, L"OwnerElement为空",L"GDMPLab",L"2024-03-30");

    if (const IExternalDataComponent* pExternalDataComponent = pOwnerElement->GetExternalDataComponent())
    {
        // ExternalData中的参数
        const std::vector<OwnerPtr<IExternalData>>& externalDatas = pExternalDataComponent->GetAllExternalDatas();
        FOR_EACH(externalData, externalDatas)
        {
            if (externalData && !externalData->IsParameterModifiable(paramDefId))
            {
                return false;
            }
        }
    }
    return true;
}

OwnerPtr<IParameter> LayoutParametersCustomizer::GetNativeParameter(int paramDefId) const
{
    IDocument* pDoc = GetDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    if (paramDefId == PARAMETER_ID(DrawingConstructionCompanyBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingConstructionCompanyBuiltInParameter), ParameterProcessType::GeneralOutput);
        opParam->SetValueAsString(L"广联达");
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(DrawingLayoutModelBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingLayoutModelBuiltInParameter), ParameterProcessType::GeneralOutput);
        opParam->SetValueAsString(L"标准模板");
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(DrawingLayoutNameBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingLayoutNameBuiltInParameter), ParameterProcessType::GeneralInput);
        const IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_UNLESS(pLayout, nullptr, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        opParam->SetValueAsString(pLayout->GetName());
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(ViewCropByBorderBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::Bool, DefaultParameterAttributes,
            PARAMETER_UID(ViewCropByBorderBuiltInParameter), ParameterProcessType::GeneralInput);
        const IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_UNLESS(pLayout, nullptr, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        const IModelView* pModelView = pLayout->GetModelView();
        DBG_WARN_AND_RETURN_UNLESS(pModelView, nullptr, L"pModelView为空",L"GDMPLab",L"2024-03-30");
        opParam->SetValueAsBool(pModelView->IsCroppedByViewport());
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(ViewBorderVisibleBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::Bool, DefaultParameterAttributes,
            PARAMETER_UID(ViewBorderVisibleBuiltInParameter), ParameterProcessType::GeneralInput);
        const IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_UNLESS(pLayout, nullptr, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        const IModelView* pModelView = pLayout->GetModelView();
        DBG_WARN_AND_RETURN_UNLESS(pModelView, nullptr, L"pModelView为空",L"GDMPLab",L"2024-03-30");
        IViewClipRange* pViewClipRange = IViewClipRange::GetViewClipRange(pModelView->GetDocument(), pModelView->GetViewClipRangeId());
        opParam->SetValueAsBool(pViewClipRange->IsClipVisible());
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(DrawingLayoutScaleBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingLayoutScaleBuiltInParameter), ParameterProcessType::GeneralOutput);
        const IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_UNLESS(pLayout, nullptr, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        std::vector<IDrawingViewport*> viewports = pLayout->GetAllViewports();
        std::wstring scales;
        if (!viewports.empty())
        {
            // 目前通过获取视口对应的modelview的比例， 后面考虑是否要对视口设置个视口比例属性。还有视口比例可以自定义后，这里也需要改动
            std::unordered_set<std::wstring> viewportScales;
            FOR_EACH(viewport, viewports)
            {
                const IModelView* pModelView = GetModelView(viewport);
                double viewportScale = pModelView->GetViewScale();
                viewportScales.insert(std::to_wstring(1) + L":" + std::to_wstring(int(1 / viewportScale))); //测试
            }
            FOR_EACH(viewportScale, viewportScales)
            {
                scales.append(viewportScale);
                scales.append(L" ");
            }
        }
        opParam->SetValueAsString(scales);
        return TransferOwnership(opParam);
    }
    return nullptr;
}

bool LayoutParametersCustomizer::SetNativeParameter(const IParameter* param, std::wstring* errorMsg)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(param, L"param为空",L"GDMPLab",L"2024-03-30");
    if (errorMsg)
    {
        errorMsg->clear();
    }
    // 内建参数
    if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingLayoutNameBuiltInParameter))
    {
        IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pLayout, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        pLayout->SetName(param->GetValueAsString());
        return true;
    }
    if (param->GetParameterDefinitionId() == PARAMETER_ID(ViewCropByBorderBuiltInParameter))
    {
        IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pLayout, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        IModelView* pModelView = pLayout->GetModelView();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
        pModelView->CropByViewport(param->GetValueAsBool());
        return true;
    }
    if (param->GetParameterDefinitionId() == PARAMETER_ID(ViewBorderVisibleBuiltInParameter))
    {
        IDrawingLayout* pLayout = GetLayout();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pLayout, L"pLayout为空",L"GDMPLab",L"2024-03-30");
        IModelView* pModelView = pLayout->GetModelView();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
        IViewClipRange* pViewClipRange = IViewClipRange::GetViewClipRange(pModelView->GetDocument(), pModelView->GetViewClipRangeId());
        pViewClipRange->SetClipVisible(param->GetValueAsBool());
        return true;
    }
    return false;
}

void LayoutParametersCustomizer::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    pParamDefIds->clear();
    pParamDefIds->push_back(PARAMETER_ID(DrawingConstructionCompanyBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(DrawingLayoutModelBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(DrawingLayoutNameBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(DrawingLayoutScaleBuiltInParameter));
}

const IDrawingLayout* LayoutParametersCustomizer::GetLayout() const
{
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    const IExternalObject* pExternalObiect = pGenericElement->GetExternalObject();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pExternalObiect, L"pExternalObiect为空",L"GDMPLab",L"2024-03-30");
    return quick_cast<IDrawingLayout>(pExternalObiect);
}

IDrawingLayout* LayoutParametersCustomizer::GetLayout()
{
    IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    IExternalObject* pExternalObiect = pGenericElement->GetExternalObject();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pExternalObiect, L"pExternalObiect为空",L"GDMPLab",L"2024-03-30");
    return quick_cast<IDrawingLayout>(pExternalObiect);
}

//获得视口对应的modelview
const IModelView* LayoutParametersCustomizer::GetModelView(const IDrawingViewport* pViewport) const
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pViewport, L"pViewport为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = GetDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    ElementId modelViewId = pViewport->GetModelViewId();
    IElement* pElement = pDoc->GetElement(modelViewId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = quick_cast<IModelView>(pElement);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    return pModelView;
}



//ViewPort的CustomizerParameters定制行为
DBOBJECT_DATA_DEFINE(ViewPortParametersCustomizer)
{
    SetOwnerElement(nullptr);
}

ViewPortParametersCustomizer::ViewPortParametersCustomizer(IElement* pElement)
    :m_pOwnerElement(pElement)
{
}

ViewPortParametersCustomizer::~ViewPortParametersCustomizer()
{
}

IDocument* ViewPortParametersCustomizer::GetDocument() const
{
    if (!m_pOwnerElement)
    {
        return nullptr;
    }
    return m_pOwnerElement->GetDocument();
}

OwnerPtr<IParameter> ViewPortParametersCustomizer::GetNativeParameter(int paramDefId) const
{
    const IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_UNLESS(pElement, nullptr, L"pElement为空",L"GDMPLab",L"2024-03-30");
    const IDrawingViewport* pViewport = GetViewport();
    DBG_WARN_AND_RETURN_UNLESS(pViewport, nullptr, L"pViewport为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pElement->GetDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IElement* pLayout = pDoc->GetElement(pViewport->GetLayoutId());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pLayout, L"pLayout为空",L"GDMPLab",L"2024-03-30");
    IElementBasicInformation* pBasicInfo = pLayout->GetBasicInformation();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pBasicInfo, L"pBasicInfo为空",L"GDMPLab",L"2024-03-30");
    std::wstring paperName = pBasicInfo->GetName();
    IElement* pViewportModelView = pDoc->GetElement(pViewport->GetModelViewId());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pViewportModelView, L"pViewportModelView为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = quick_cast<IModelView>(pViewportModelView);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    if (paramDefId == PARAMETER_ID(DrawingViewPortNameBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingViewPortNameBuiltInParameter), ParameterProcessType::GeneralInput);
        opParam->SetValueAsString(pViewport->GetName());
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(ViewScaleBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::Double, DefaultParameterAttributes,
            PARAMETER_UID(ViewScaleBuiltInParameter), ParameterProcessType::GeneralInput);
        opParam->SetValueAsDouble(pModelView->GetViewScale());
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(DrawingLayoutNameBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingLayoutNameBuiltInParameter), ParameterProcessType::GeneralOutput);
        opParam->SetValueAsString(paperName);
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(DrawingViewPortLockBuiltInParameter)
        && (!pViewport->IsSupportsOneToMany()))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::Int, DefaultParameterAttributes,
            PARAMETER_UID(DrawingViewPortLockBuiltInParameter), ParameterProcessType::GeneralInput);
        opParam->SetValueAsInt(pViewport->IsLocked() ? 1 : 0);
        return TransferOwnership(opParam);
    }
    return nullptr;
}

bool ViewPortParametersCustomizer::SetNativeParameter(const IParameter *param, std::wstring* errorMsg)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");
    if (errorMsg)
    {
        errorMsg->clear();
    }

    IElement* pElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");
    IDrawingViewport* pViewport = GetViewportFW();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pViewport, L"pViewport为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pElement->GetDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    IElement* pViewportModelView = pDoc->GetElement(pViewport->GetModelViewId());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pViewportModelView, L"pViewportModelView为空",L"GDMPLab",L"2024-03-30");
    IModelView* pModelView = quick_cast<IModelView>(pViewportModelView);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    // 内建参数
    if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingViewPortNameBuiltInParameter))
    {
        pViewport->SetName(param->GetValueAsString());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(ViewScaleBuiltInParameter))
    {
        pModelView->SetViewScale(param->GetValueAsDouble());
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingViewPortIndexBuiltInParameter))
    {
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingLayoutNameBuiltInParameter))
    {
        return true;
    }
    else if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingViewPortLockBuiltInParameter)
        &&(!pViewport->IsSupportsOneToMany()))
    {
        if (param->GetValueAsInt())
        {
            pViewport->Lock();
        }
        else
        {
            pViewport->Unlock();
        }
        return true;
    }
    return false;
}

void ViewPortParametersCustomizer::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    pParamDefIds->clear();
    pParamDefIds->push_back(PARAMETER_ID(DrawingViewPortNameBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(ViewScaleBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(DrawingLayoutNameBuiltInParameter));
    const IDrawingViewport* pViewport = GetViewport();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pViewport, L"pViewport为空",L"GDMPLab",L"2024-03-30");
    if (!pViewport->IsSupportsOneToMany())
    {
        pParamDefIds->push_back(PARAMETER_ID(DrawingViewPortLockBuiltInParameter));
    }
}

bool ViewPortParametersCustomizer::IsParameterModifiable(int paramDefId) const
{
    if (paramDefId == PARAMETER_ID(DrawingLayoutNameBuiltInParameter))
    {
        return false;
    }
    if (paramDefId == PARAMETER_ID(DrawingViewPortNameBuiltInParameter))
    {
        return false;
    }
    if (paramDefId == PARAMETER_ID(DrawingViewPortIndexBuiltInParameter))
    {
        return false;
    }
    return true;
}
const IDrawingViewport* ViewPortParametersCustomizer::GetViewport() const
{
    const IElement* pElement = GetOwnerElement();
    if (pElement == nullptr)
    {
        return nullptr;
    }
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    if (pGenericElement == nullptr)
    {
        return nullptr;
    }
    const IExternalObject* pExternalObiect = pGenericElement->GetExternalObject();
    if (pExternalObiect == nullptr)
    {
        return nullptr;
    }
    const IDrawingViewport* pViewport = quick_cast<IDrawingViewport>(pExternalObiect);
    return pViewport;
}
IDrawingViewport* ViewPortParametersCustomizer::GetViewportFW()
{
    IElement* pElement = GetOwnerElement();
    if (pElement == nullptr)
    {
        return nullptr;
    }
    IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    if (pGenericElement == nullptr)
    {
        return nullptr;
    }
    IExternalObject* pExternalObiect = pGenericElement->GetExternalObject();
    if (pExternalObiect == nullptr)
    {
        return nullptr;
    }
    IDrawingViewport* pViewport = quick_cast<IDrawingViewport>(pExternalObiect);
    return pViewport;
}
DBOBJECT_DATA_DEFINE(ViewportScaleOverride)
{
}

DBOBJECT_DATA_DEFINE(ViewportCropByBorderOverride)
{
}


DBOBJECT_DATA_DEFINE(ViewportBorderVisibleOverride)
{
}



//DrawingViewportType的CustomizerParameters定制行为
DBOBJECT_DATA_DEFINE(DrawingViewportTypeParametersCustomizer)
{
    SetOwnerElement(nullptr);
}

IDocument* DrawingViewportTypeParametersCustomizer::GetDocument() const
{
    if (!m_pOwnerElement)
    {
        return nullptr;
    }
    return m_pOwnerElement->GetDocument();
}

bool DrawingViewportTypeParametersCustomizer::IsParameterModifiable(int paramDefId) const
{
    const IElement* pOwnerElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pOwnerElement, L"OwnerElement为空",L"GDMPLab",L"2024-03-30");

    if (const IExternalDataComponent* pExternalDataComponent = pOwnerElement->GetExternalDataComponent())
    {
        // ExternalData中的参数
        const std::vector<OwnerPtr<IExternalData>>& externalDatas = pExternalDataComponent->GetAllExternalDatas();
        FOR_EACH(externalData, externalDatas)
        {
            if (externalData && !externalData->IsParameterModifiable(paramDefId))
            {
                return false;
            }
        }
    }
    return true;
}

OwnerPtr<IParameter> DrawingViewportTypeParametersCustomizer::GetNativeParameter(int paramDefId) const
{
    IDocument* pDoc = GetDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    if (paramDefId == PARAMETER_ID(DrawingViewportTypeTitleBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::String, DefaultParameterAttributes,
            PARAMETER_UID(DrawingViewportTypeTitleBuiltInParameter), ParameterProcessType::GeneralOutput);
        opParam->SetValueAsString(L"广联达视口");
        return TransferOwnership(opParam);
    }
    if (paramDefId == PARAMETER_ID(DrawingViewportTypeDisplayTitleBuiltInParameter))
    {
        OwnerPtr<IParameter> opParam = IParameter::CreateParameter(pDoc, ParameterStorageType::Int, DefaultParameterAttributes,
            PARAMETER_UID(DrawingViewportTypeDisplayTitleBuiltInParameter), ParameterProcessType::GeneralInput);
        opParam->SetValueAsInt(1);
        return TransferOwnership(opParam);
    }
    return nullptr;
}

bool DrawingViewportTypeParametersCustomizer::SetNativeParameter(const IParameter *param, std::wstring* errorMsg)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param为空",L"GDMPLab",L"2024-03-30");
    if (errorMsg)
    {
        errorMsg->clear();
    }
    // 内建参数
    if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingViewportTypeTitleBuiltInParameter))
    {
        //IDrawingLayout* pLayout = GetLayout();
        //DBG_WARN_AND_RETURN_FALSE_UNLESS(pLayout, L"pLayout为空", L"GDMPLab", L"2023-12-20");
        //pLayout->SetName(param->GetValueAsString());
        return true;
    }
    if (param->GetParameterDefinitionId() == PARAMETER_ID(DrawingViewportTypeDisplayTitleBuiltInParameter))
    {
        return true;
    }
    return false;
}

void DrawingViewportTypeParametersCustomizer::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pParamDefIds, L"pParamDefIds为空",L"GDMPLab",L"2024-03-30");
    pParamDefIds->clear();
    pParamDefIds->push_back(PARAMETER_ID(DrawingViewportTypeTitleBuiltInParameter));
    pParamDefIds->push_back(PARAMETER_ID(DrawingViewportTypeDisplayTitleBuiltInParameter));
}
